package com.example.projectmanagement.repository.study;

import com.example.projectmanagement.model.study.UserLessonProgress;
import com.example.projectmanagement.model.User;
import com.example.projectmanagement.model.study.Lesson;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Optional;

@Repository
public interface UserLessonProgressRepository extends JpaRepository<UserLessonProgress, Long> {
    Optional<UserLessonProgress> findByUserAndLesson(User user, Lesson lesson);
    List<UserLessonProgress> findByUserAndLesson_Module_Course_Id(User user, Long courseId);
    List<UserLessonProgress> findByLesson_Module_Id(Long moduleId);
    
    @Query("SELECT ulp FROM UserLessonProgress ulp WHERE ulp.user.id = :userId AND ulp.lesson.module.course.id = :courseId AND ulp.status = 'COMPLETED'")
    List<UserLessonProgress> findCompletedLessonsByUserAndCourse(@Param("userId") Long userId, @Param("courseId") Long courseId);
    
    @Query("SELECT COUNT(ulp) FROM UserLessonProgress ulp WHERE ulp.user.id = :userId AND ulp.lesson.module.course.id = :courseId AND ulp.status = 'COMPLETED'")
    Integer countCompletedLessonsByUserAndCourse(@Param("userId") Long userId, @Param("courseId") Long courseId);
}